import java.io.*;
import java.util.ArrayList;

/**
 * User: rmikhaylenko mirusnet@gmail.com
 * Date: 09/08/13
 * Time: 13:14
 */
public class PlaylistUtils {


    public static ArrayList<String> getPlaylists(String fullPlaylistsPath) {
        CommonIO.checkDirAvailability(fullPlaylistsPath);
        ArrayList<String> list = new ArrayList<>();

        File dir_name = new File(fullPlaylistsPath);

        File[] files = dir_name.listFiles();
        if (files != null) {
            for (File f : files) {
                if (f.isDirectory()) {
                    continue;
                } else {
                    if (f.getName().endsWith(".playlist")) {
                        list.add(f.getAbsolutePath());
                    }
                }
            }
        }
        return list;
    }


    /*
        Returns the list with full filepaths starting from the skipFiles+1 position
     */
    public static ArrayList<String> getPlaylist(String fullPlaylistPath, int startPosition) {
        ArrayList<String> list = new ArrayList<>();
        CommonIO.checkFileAvailability(fullPlaylistPath);
        File file = new File(fullPlaylistPath);
        String line;
        int lineNumber = 1;

        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            while ((line = br.readLine()) != null) {
                if (lineNumber < startPosition) {
                    lineNumber++;
                    continue;
                } else {
                    list.add(line);
                    lineNumber++;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return list;
    }

    public static int getCountOfFilesInPlaylist(String fullPlaylistPath) {
        CommonIO.checkFileAvailability(fullPlaylistPath);
        File file = new File(fullPlaylistPath);
        String line;
        int fileCount = 0;

        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            while ((line = br.readLine()) != null) {
                fileCount++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return fileCount;
    }


    //returns alf.jpg (example)
    public static String getIconName(String playlistPath) {
        String iconFileName = CommonIO.getFileNameWithOutExtention(playlistPath) + Config.getDefaultIconExtention();
        CommonIO.checkFileAvailability(Config.getMplayerConfigDir() + iconFileName);

        String line = null;

        try (BufferedReader br = new BufferedReader(new FileReader(new File(Config.getMplayerConfigDir() + iconFileName)))) {
            line = br.readLine();
            if (line == null) {
                System.out.println("Unable to get Icon from file " + Config.getMplayerConfigDir() + iconFileName);
                System.exit(1);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return line;
    }

    public static String getIconHoverName(String iconFileName) {
        String[] temp = iconFileName.split("[.]+");
        return temp[0] + Config.getDefaultHoverSuffix() + "." + temp[1];
    }

    public static int getStartPosition(String playlistPath) {
        String positionFileName = CommonIO.getFileNameWithOutExtention(playlistPath) + Config.getDefaultPositionExtention();
        CommonIO.checkFileAvailability(Config.getMplayerConfigDir() + positionFileName);

        String line;
        int position = 0;

        try (BufferedReader br = new BufferedReader(new FileReader(new File(Config.getMplayerConfigDir() + positionFileName)))) {
            line = br.readLine();
            if (line == null) {
                System.out.println("Unable to get position from file " + Config.getMplayerConfigDir() + positionFileName);
                System.exit(1);
            } else {
                String[] temp = line.split("[|]+");
                position = Integer.parseInt(temp[0]);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return position;
    }

    public static String getSecondsPosition(String playlistPath) {
        String positionFileName = CommonIO.getFileNameWithOutExtention(playlistPath) + Config.getDefaultPositionExtention();
        CommonIO.checkFileAvailability(Config.getMplayerConfigDir() + positionFileName);

        String line;
        String position = null;

        try (BufferedReader br = new BufferedReader(new FileReader(new File(Config.getMplayerConfigDir() + positionFileName)))) {
            line = br.readLine();
            if (line == null) {
                System.out.println("Unable to get position from file " + Config.getMplayerConfigDir() + positionFileName);
                System.exit(1);
            } else {
                String[] temp = line.split("[|]+");
                return temp[1];
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return position;
    }

    //ANS_time_pos=87.0800000 => 87.0800000
    public static String parseSeconds(String line) {
        String[] fields = line.split("[=]+");
        return fields[1];
    }

    public static void changeSecondsAndTrackInPlaylist(String seconds, int track, String playlistPath) {
        String positionFileName = CommonIO.getFileNameWithOutExtention(playlistPath);
        String fullPositionFileName = Config.getMplayerConfigDir() + positionFileName + Config.getDefaultPositionExtention();
        CommonIO.checkFileAvailability(fullPositionFileName);

        try {
            FileWriter fw = new FileWriter(fullPositionFileName);
            fw.write(track + "|" + seconds);
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return;
    }

}

